﻿using ExcelDemo.Handle;
using ExcelDemo.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ExcelDemo
{
    /// <summary>
    /// 针对Excel写的帮助模型
    /// </summary>
    public partial class Form1 : Form
    {
        #region 变量


        /// <summary>
        /// 导出模板文件名称
        /// </summary>
        string FileName = "导出模板";

        /// <summary>
        /// Excel底层页签名称
        /// </summary>
        string SheetName = "Sheet1|Sheet2";

        private static DataTable dtRule = new DataTable();

        #endregion

        #region 初始化、数据加载
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region 控件事件
        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            //对于选择文件转化的DataTable和提示信息msg的委托
            Action<string, List<DataTable>> action = new Action<string, List<DataTable>>((string str, List<DataTable> dt) =>
            {
                if (dt == null || dt[0].Rows.Count == 0)
                {
                    MessageBox.Show("dt为空的");
                    return;
                }

                if (dt[0].Rows.Count > 5000)
                {
                    MessageBox.Show("导入的数据已超过最大限制5000条");
                    return;
                }
                dt[0].AcceptChanges();
                dt[1].AcceptChanges();
                this.dgv.DataSource = dt[0];
                dtRule = dt[1];
                //this.dgvrule.DataSource = dt[1];
            });
            AsposeExcel.ImportExcelToDataTable(this, action, this.SheetName);
        }

        /// <summary>
        /// 导出模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOut_Click(object sender, EventArgs e)
        {
            string url = System.AppDomain.CurrentDomain.BaseDirectory + "TempFile\\模板.xlsx";
            string remoteUri = System.IO.Path.GetDirectoryName(url);
            string fileName = System.IO.Path.GetFileName(url);
            string myStringWebResource = null;
            WebClient myWebClient = new WebClient();
            myStringWebResource = url;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            //dialog.Description = "请选择下载路径";
            string foldPath = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foldPath = dialog.SelectedPath;
                string fullfile = foldPath + "\\" + fileName;
                myWebClient.DownloadFile(myStringWebResource, fullfile);
                MessageBox.Show("下载成功");
            }
        }

        #endregion

        private void btnSumScore_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)dgv.DataSource;
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("请导入数据后再计算！");
                    return;
                }
                // DataTable ruledt = (DataTable)dgvrule.DataSource;
                DataTable newDt = CaclHelper.GetCaclScoreDt(dt, dtRule);
                dgv.DataSource = newDt;
                //this.label3.Text = "分数计算完毕";
                //this.label3.ForeColor = Color.Red;
                MessageBox.Show("分数计算完毕");
            }
            catch (Exception ex)
            {
                MessageBox.Show("请导入数据后再计算！");
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dgv.DataSource;
            try
            {

                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("请导入数据后再导出结果！");
                    return;
                }

                SaveFileDialog sfd = new SaveFileDialog();
                string Filter = "Excel文件|*.csv|Excel文件|*.xls|Excel文件|*.xlsx";
                sfd.Filter = Filter;
                sfd.FilterIndex = 3;
                string tempPath = System.AppDomain.CurrentDomain.BaseDirectory + "TempFile\\ScoreTemp.xlsx";
                this.label3.Text = "结果导出中，请耐心等待。。。";
                AsposeExcel.TemplateExportExcel(tempPath, dt);
                this.label3.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("请导入数据后再导出结果！");
            }
            //if (sfd.ShowDialog() == DialogResult.OK)
            //{
            //   
            //    fileName = sfd.FileName; //导入 
            //if (ExcelHelper.outPutExcel(fileName, dt))
            //    {
            //        this.label3.Text = "";
            //        MessageBox.Show("导出成功！");
            //    }
            //}
        }


        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="filename"></param>
        public void DownloadFile(string URL, string filename)
        {
            AsposeExcel.DownloadFile(URL, filename, null);
        }

        private void btnRule_Click(object sender, EventArgs e)
        {
            if (dtRule.Rows.Count == 0)
            {
                MessageBox.Show("未导入数据，无评分规则！");
                return;
            }
            Form2 frm2 = new Form2(dtRule);
            frm2.ShowDialog();
        }
    }
}
